package com.bqtv.sport.ui.home.fragment

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import androidx.viewbinding.ViewBinding
import com.bqtv.sport.R
import com.bqtv.sport.adapter.BannerRoundImageAdapter
import com.bqtv.sport.adapter.HomeHotAdapter
import com.bqtv.sport.adapter.HomeTodayAdapter
import com.bqtv.sport.app.AppActivity
import com.bqtv.sport.app.AppFragment
import com.bqtv.sport.commen.widget.GridItemDecoration
import com.bqtv.sport.commen.widget.LinearItemDecoration
import com.bqtv.sport.databinding.FragmentHomeRecommendBinding
import com.bqtv.sport.ui.home.LiveDetailActivity
import com.youth.banner.indicator.RectangleIndicator
import com.youth.banner.listener.OnBannerListener


class HomeRecommendFragment :
    AppFragment<FragmentHomeRecommendBinding, AppActivity<ViewBinding>>() {
    private lateinit var todayAdapter: HomeTodayAdapter
    private lateinit var hotAdapter: HomeHotAdapter

    companion object {
        private const val INTENT_KEY_TYPE = "type"
        fun newInstance(): HomeRecommendFragment {
            val fragment = HomeRecommendFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            return fragment
        }

        fun newInstance(type: String?): HomeRecommendFragment {
            val fragment = HomeRecommendFragment()
            val bundle = Bundle()
            bundle.putString(INTENT_KEY_TYPE, type)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun getViewBinding() = FragmentHomeRecommendBinding.inflate(layoutInflater)

    override fun initView() {
        binding.recyclerToday.layoutManager =
            LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false)

        binding.recyclerToday.addItemDecoration(LinearItemDecoration(getContext(), LinearItemDecoration.LineDrawMode.HORIZONTAL,16f,10f,10f))
        todayAdapter = HomeTodayAdapter()
        binding.recyclerToday.adapter = todayAdapter

        binding.recyclerHot.layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        binding.recyclerHot.addItemDecoration(GridItemDecoration(getContext(), 2,
            GridItemDecoration.LineDrawMode.HORIZONTAL,10,15))

        hotAdapter = HomeHotAdapter()
        binding.recyclerHot.adapter = hotAdapter
    }

    override fun initData() {

        val list: MutableList<String> = ArrayList()
        list.add("1")
        list.add("2")
        list.add("3")
        list.add("4")
        binding.banner.setIndicator(RectangleIndicator(getContext()))
        val bannerAdapter: BannerRoundImageAdapter<String> =
            object : BannerRoundImageAdapter<String>(list) {

                override fun onBindView(
                    holder: BannerRoundImageAdapter<*>.BannerRoundImageHolder?,
                    data: String,
                    position: Int,
                    size: Int
                ) {
                    val bannerBean: String = data as String
                    (holder as BannerRoundImageAdapter<*>.BannerRoundImageHolder).imageView.setImageResource(
                        R.mipmap.ic_banner
                    )
//                GlideApp.with(context!!)
//                    .load(R.mipmap.ic_banner)
//                    .into((holder as BannerRoundImageAdapter<*>.BannerRoundImageHolder).imageView)
                }
            }
        bannerAdapter.setOnBannerListener(object : OnBannerListener<String> {
            override fun OnBannerClick(data: String, position: Int) {

            }
        })
        binding.banner.setAdapter(bannerAdapter)
        binding.banner.addBannerLifecycleObserver(this)
//        binding.banner.setBannerGalleryMZ(16)

        todayAdapter.submitList(list)
        hotAdapter.submitList(list)

        initListener()
    }

    private fun initListener() {
        hotAdapter.setOnItemClickListener({ adapter, view, position ->
            LiveDetailActivity.start(getAttachActivity()!!, 1);
        })
    }
}

